function BoxBoundUpdate(){
    var x = this.pos[0] + this.vel[0];
    var y = this.pos[1] + this.vel[1];
    var flag = false;

    if((x-this.r) > 0 && (x+this.r) < canvas.width){
        this.pos[0] = x;
    }else{
        this.vel[0] = -this.vel[0];
        flag = true;
    }

    if((y-this.r) > 0 && (y+this.r)< canvas.height){
        this.pos[1] = y;
    }else{
        this.vel[1] = -this.vel[1];
        flag = true;
    }        
    return flag;    //true means collision occured.
}

function CircleCollision(obj){
    if(obj.r){
        var xd = this.pos[0] - (obj.pos[0]+map.pos[0]);
        var yd = this.pos[1] - (obj.pos[1]+map.pos[1]);

        if(Math.sqrt(xd * xd + yd * yd) < (obj.r + this.r)){
            return true;
        }
        return false;
    }
    alert("kufli object sent to Circle Collision");
}

function drawCircle( c1, c2 ){
    ctx.fillStyle = c1; 
    ctx.beginPath();
    ctx.arc(this.pos[0], this.pos[1], this.r, 0, Math.PI*2, true);
    ctx.closePath();
    ctx.fill();
    ctx.lineWidth = 5;
    ctx.fillStyle = "#000000";
    ctx.strokeStyle = c2;
    ctx.beginPath();
    ctx.arc(this.pos[0], this.pos[1], this.r, 0, Math.PI*2, false);
    ctx.stroke();
}

function Map(x, y, r){
    this.pos = new Array(2);
    this.pos[0] = x;
    this.pos[1] = y;
    this.vel = new Array(2);
    this.vel[0] = 0;
    this.vel[1] = 0;
    this.r = r;

    this.update = BoxBoundUpdate;

    this.draw = function(a, b){
        ctx.drawImage(data.img_map, this.pos[0]-this.r, this.pos[1]-this.r, 2*this.r, 2*this.r);
    }
        //drawCircle; 
}

function Pickup(img, pos){
    this.pos = new Array(2);
    var angle = Math.random() * Math.PI * 2;
    var radius = Math.random() * map.r; 
    this.pos[0] = pos[0]; //map.pos[0] + radius * Math.cos(angle);
    this.pos[1] = pos[1]; //map.pos[1] + radius * Math.sin(angle);
    this.r = 8;
    this.img= img;//data.img_pickup;
    this.moves = false; //set this to true to call the update
    this.power_type = null;
    this.start_time = null;
//    console.log("Adding: ", this.name, this.pos[0], this.pos[1]);

    this.update = function(){
        //this function is here only for the moving enemy type.
        var hx = hero.pos[0] - map.pos[0];
        var hy = hero.pos[1] - map.pos[1];
        var dx = this.pos[0] - hx;
        var dy = this.pos[1] - hy;
        var denom = Math.sqrt(dx * dx + dy * dy);
        var vx = 2 * -dx/denom;
        var vy = 2 * -dy/denom;
        //move pickup along with the map.
        this.pos[0] = this.pos[0] + vx;
        this.pos[1] = this.pos[1] + vy;
    }

    this.draw = function(a, b){
        //ctx.drawImage(data.img_pickup, this.pos[0]-this.r, this.pos[1]-this.r, 2*this.r, 2*this.r);
        try{
//        console.log("Rendering: ", this.name, this.pos[0], this.pos[1]);
        ctx.drawImage(this.img, map.pos[0]+this.pos[0]-this.r, map.pos[1] + this.pos[1]-this.r, 2*this.r, 2*this.r);
        }catch(e){
            console.log(e);
        }

    }
        //drawCircle;
}


function Hero(){
    //variables for tracking the achievements
    this.pickups = 0;
    this.crushmiss = 0;
    this.tmn = 0;
    //--------------------
    this.pos = new Array(2);
    var angle = Math.random() * Math.PI * 2;
    this.pos[0] = map.pos[0] + map.r * Math.cos(angle);
    this.pos[1] = map.pos[1] + map.r * Math.sin(angle);
    this.vel = new Array(2);
    this.vel[0] = 0;
    this.vel[1] = 0;
    this.r = 15; 
    this.speed = 5;

    this.update = function(){
        var x = this.vel[0] + this.pos[0] + map.vel[0];
        var y = this.vel[1] + this.pos[1] + map.vel[1];
        var xdiff = (x-map.pos[0]);
        var ydiff = (y-map.pos[1]); 
        if((xdiff * xdiff + ydiff * ydiff) > (map.r * map.r)){
            x -= this.vel[0];
            y -= this.vel[1];
            this.vel[0] = this.vel[1] = 0;
        }
        this.pos[0] = x;
        this.pos[1] = y;
    }
    this.draw = function(a, b){
        try{
        ctx.drawImage(data.img_hero, this.pos[0]-this.r, this.pos[1]-this.r, 2*this.r, 2*this.r);
        }catch(e){
            alert(e);
        }        
    }//drawCircle;

    this.cCollision = CircleCollision;
    this.bCollision = function(pos, dims){
        x = pos[0];
        y = pos[1];
        w = dims[0];
        h = dims[1];
        if((this.pos[0]-this.r) > x && (this.pos[0] + this.r) < (x+w)){
            if((this.pos[1]-this.r) > y && (this.pos[1]+this.r) < (y+h)){
                    return false;
            }
        }
        return true;
    }
}
